//
//  XL_NewsHeadView.m
//  OnePointDemo
//
//  Created by 薛磊 on 2018/7/4.
//  Copyright © 2018年 XL. All rights reserved.
//

#import "XL_NewsHeadView.h"

@interface XL_NewsHeadView()<UIScrollViewDelegate>
{
    UIButton * _allButton;
}
@property(nonatomic,strong) NSArray * souceArray;
@property(nonatomic,strong) UIScrollView * headScrollView;
@property(nonatomic,strong) UIView * lineView;

@end


@implementation XL_NewsHeadView

-(instancetype)initWithCreatViewArray:(NSArray *)array rect:(CGRect)rect{
    
    if (self = [super initWithFrame:rect]) {

        self.souceArray = array;
        
        [self creatView];
    }
    
    return self;
}
-(void)creatView{
    
    self.headScrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    
    self.headScrollView.scrollEnabled = YES;
    
    self.headScrollView.bounces = NO;

    self.headScrollView.showsHorizontalScrollIndicator = NO;
    
    self.headScrollView.pagingEnabled = NO;
    
    self.headScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame) /4* self.souceArray.count, 50);
    
    [self addSubview:self.headScrollView];
    
    for (int index = 0; index < self.souceArray.count; index ++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.showsTouchWhenHighlighted = YES;
        [button setTitle:self.souceArray[index] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
        if (index == 0) {
            _allButton = button;
            _allButton.selected = YES;
        }
        button.tag = index;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.headScrollView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.frame.size.width/4 * index);
            make.top.bottom.mas_equalTo(0);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(self.frame.size.width/4);
            if (index == self.souceArray.count - 1) {
                make.right.mas_equalTo(0);
            }
            
        }];
        
        
    }
    
    self.lineView = [UIView new];
    self.lineView.backgroundColor = [UIColor orangeColor];
    [self addSubview:self.lineView];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(UIScreenWIDTH/4);
        
    }];
}
-(void)btnClick:(UIButton *)button{
    
    if (button.selected == YES) {
        return;
    }
    _allButton.selected = NO;
    _allButton = button;
    _allButton.selected = YES;
    
    
    
    CGFloat width = 0.0;
    CGFloat lineWidth = 0.0;
    
    switch (button.tag) {
        case 0:
            width = 0;
            lineWidth = 0;
            break;
        case 1:
            width = 0;
            lineWidth = UIScreenWIDTH/4;
            break;
        case 2:
            width = UIScreenWIDTH/4;
            lineWidth = UIScreenWIDTH/4;
            break;
        case 3:
            width = UIScreenWIDTH/4 *2;
            lineWidth = UIScreenWIDTH/4;
            break;
        case 4:
            width = UIScreenWIDTH/4 *2;
            lineWidth = UIScreenWIDTH/4 * 2;
            break;
        case 5:
            width = UIScreenWIDTH/4 *2;
            lineWidth = UIScreenWIDTH/4 * 3;
            break;
            
        default:
            break;
    }
    
    [UIView animateWithDuration:0.4 animations:^{
        self.headScrollView.contentOffset = CGPointMake(width, 0);
        [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(lineWidth);
            
        }];
    }];

   
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(headViewClickButton:)]) {
        [self.delegate headViewClickButton:button];
    }
    
    
}
//-(void)drawRect:(CGRect)rect{
//    [super drawRect:rect];
//
//    self.layer = [[CALayer alloc]init];
//
//    self.layer.backgroundColor = [UIColor orangeColor].CGColor;
//
//    self.layer.frame = CGRectMake(0, 49, UIScreenWIDTH/4, 1);
//
//    [self.layer addSublayer:self.layer];
//
//}
@end
